package com.fg.exemplocrudengenhariadesoftware.daobd;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.apache.log4j.Logger;

import com.fg.exemplocrudengenhariadesoftware.model.Turma;

public class TurmaDao {
	private JDBCUitl JDBCUitl;
	private Connection conexaoBD;
	Logger logger;
	
	public TurmaDao() {
		JDBCUitl = new JDBCUitl();
		logger = Logger.getLogger(this.getClass().getName());
	}
	
	public void insere (Turma turma) throws ErroAoConectarBDException {
		PreparedStatement preparedStatement = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "INSERT INTO tbTURMA (TURMA) VALUES (?)";
				logger.debug("insert:" + stringSQL);
				try {
					preparedStatement = conexaoBD.prepareStatement(stringSQL);
					preparedStatement.setInt(1, turma.getTurma());
					preparedStatement.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao inserir Aluno:" + turma.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}
	}
	public void delete (Turma turma) throws ErroAoConectarBDException {
		PreparedStatement preparedStatement = null;
		String stringSQL = "";
		
		try {
			conexaoBD = JDBCUitl.getConexaoBd();
			if (conexaoBD != null) {
				stringSQL = "delete tbTURMA where TURMA = ?";
				logger.debug("delete:" + stringSQL);
				try {
					preparedStatement = conexaoBD.prepareStatement(stringSQL);
					preparedStatement.setInt(1, turma.getTurma());
					preparedStatement.executeUpdate();
				} catch (Exception e1) {
					logger.error("erro ao excluir Aluno:" + turma.toString());
				}
			}
		} catch (Exception e) {
			throw new ErroAoConectarBDException();
		}
	}

}
